package com.draemoncode.weixindb.service;

import com.draemoncode.weixindb.constant.AccountChangeActionConstant;
import com.draemoncode.weixindb.domain.B_Member;
import com.draemoncode.weixindb.domain.B_Member_Account_Log;
import com.draemoncode.weixindb.repository.B_MemberRepository;
import com.draemoncode.weixindb.repository.B_Member_Account_LogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class MemberService {

    @Autowired
    private B_MemberRepository b_memberRepository;

    @Autowired
    private B_Member_Account_LogRepository b_member_account_logRepository;

    public B_Member newMember(String name){
        B_Member newB_Member = new B_Member();
        newB_Member.setName(name);
        newB_Member.setMoney(new BigDecimal(0));
        B_Member save = b_memberRepository.save(newB_Member);
        return save;
    }

    public void changeAccount(B_Member b_member, Integer action, BigDecimal amount, String commet, Integer podId, Integer tiXianId){
        BigDecimal newMoney = b_member.getMoney().add(amount);
        B_Member_Account_Log b_member_account_log = new B_Member_Account_Log();
        b_member_account_log.setMoney(amount);
        b_member_account_log.setCommet(commet);
        b_member_account_log.setAction(action);
        b_member_account_log.setMember_id(b_member.getId());
        b_member_account_log.setMember_name(b_member.getName());
        b_member_account_log.setTixian_id(tiXianId);
        b_member_account_log.setPod_id(podId);
        b_member_account_log.setAccout(newMoney);
        b_member.setMoney(newMoney);
        b_member.setActive_time(new Date().getTime());
        b_memberRepository.save(b_member);
        b_member_account_logRepository.save(b_member_account_log);
    }

    public void changeAccountByPod(B_Member b_member, BigDecimal amount, Integer podId){
        changeAccount(b_member, AccountChangeActionConstant.JOIN_POD, amount, null, podId, null);
    }

    public void changeAccountByTiXian(B_Member b_member, BigDecimal amount, Integer tixianId){
        changeAccount(b_member, AccountChangeActionConstant.TI_XIAN, amount, null, null, tixianId);
    }

    public void changeAccountByChongZhi(B_Member b_member, BigDecimal amount){
        changeAccount(b_member, AccountChangeActionConstant.CHONG_ZHI, amount, null, null, null);
    }

    public void changeAccountByDaiKuan(B_Member b_member, BigDecimal amount){
        changeAccount(b_member, AccountChangeActionConstant.DAI_KUAN, amount, null, null, null);
    }

}
